<?php
namespace app\english\controller;

use think\Db;

class Index
{   
    public function index()
    {   

    	$host = "http://ali-reciteword.showapi.com";
	    $path = "/list";
	    $method = "GET";
	    $appcode = "7267b66f8de442cb9df4c61ef0f535d5";
	    $headers = array();
	    array_push($headers, "Authorization:APPCODE " . $appcode);
	    $querys = "name=";
	    $bodys = "";
	    $url = $host . $path . "?" . $querys;

	    $curl = curl_init();
	    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
	    curl_setopt($curl, CURLOPT_URL, $url);
	    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
	    curl_setopt($curl, CURLOPT_FAILONERROR, false);
	    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	    curl_setopt($curl, CURLOPT_HEADER, false);
	    if (1 == strpos("$".$host, "https://"))
	    {
	        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
	        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
	    }
        $list = json_decode(curl_exec($curl),true);
        $list = $list['showapi_res_body']['typeList'];
        // echo "<pre>";
        // dump($list);
	    foreach ($list as $key => $val) {
	        $data['title']       = $val['title'];
	        $data['course_num']  = $val['course_num'];
	        $data['word_num']    = $val['word_num'];
	        $data['pid']         = 0;
            $data['class_id']    = 0;
	        $id = Db::name('english_cat')->insertGetId($data);


	    	foreach ($val['child_list'] as $k => $v) {
	    		$chind_data['pid']        = $id;
	    		$chind_data['title']      = $v['title'];
	    		$chind_data['course_num'] = $v['course_num'];
	    		$chind_data['word_num']   = $v['word_num'];
	    		$chind_data['class_id']   = $v['class_id'];
	    		$result = Db::name('english_cat')->insertGetId($chind_data);
	    	
	    	}
	    }
	}

	public function test(){
		set_time_limit(0);


		$cat_list = Db::name('english_cat')->where('pid','neq',0)->where('id','>',347)->order('id asc')->select();
		
		foreach ($cat_list as $key => $val) {
			$class_id = $val['class_id'];

			for ($i = 0; $i < $val['course_num']+1; $i++) { 

				$result = $this->http_curl($class_id,$i);
					if($result){
						foreach ($result as $k => $v) {
						
						$data['cat_id']      = $val['id'];
						$data['class_title'] = $v['class_title'];
						$data['course_num']  = $v['course'];
						$data['symbol']      = $v['symbol'];
						$data['class_id']    = $v['class_id'];
						$data['name']        = $v['name'];
						$data['desc']        = $v['desc'];
						$data['sound']       = $v['sound'];
						if(!Db::name('english_word')->where('cat_id',$val['id'])->where('course_num',$v['course'])->where('name',$v['name'])->find()){
						    Db::name('english_word')->insert($data);
						}
					}
				}
				

			}
		}

		echo "123123";
		
        // $result = $this->http_curl(136000,1);
        // echo "<pre>";
        // print_r($result);
	}

	public function http_curl($class_id,$course){
		$host = "http://ali-reciteword.showapi.com";
	    $path = "/wordlist";
	    $method = "GET";
	    $appcode = "7267b66f8de442cb9df4c61ef0f535d5";
	    $headers = array();
	    array_push($headers, "Authorization:APPCODE " . $appcode);
	    $querys = "class_id=".$class_id."&course=".$course;
	    $bodys = "";
	    $url = $host . $path . "?" . $querys;

	    $curl = curl_init();
	    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
	    curl_setopt($curl, CURLOPT_URL, $url);
	    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
	    curl_setopt($curl, CURLOPT_FAILONERROR, false);
	    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	    curl_setopt($curl, CURLOPT_HEADER, false);
	    if (1 == strpos("$".$host, "https://"))
	    {
	        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
	        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
	    }
        $list = json_decode(curl_exec($curl),true);
        return $list['showapi_res_body']['list'];
	}
}
